package view;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import types.LogOnArguments;

import net.miginfocom.swing.MigLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class LogOnDialog extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3852890208631244251L;
	private JPanel contentPane;
	private JTextField textFieldSerwer;
	private JLabel lblNewLabel;
	private JTextField textFieldPort;
	private JLabel lblNick;
	private JTextField textFieldNick;
	private JCheckBox chckbxSLL;
	private JCheckBox chckbxSzyfrowaniePakietow;
	private JLabel lblKlucz;
	private JTextField textFieldKlucz;
	private JButton btnPolacz;
	
	private LogOnListener mLogOnListener;

	/**
	 * Create the frame.
	 */
	public LogOnDialog() {
		setTitle("EloEsoChat - Logowanie");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 319);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new MigLayout("", "[grow][][center][grow]", "[grow][][][][][][][][][grow]"));
		
		JLabel lblServer = new JLabel("Serwer");
		contentPane.add(lblServer, "cell 1 1,alignx trailing");
		
		textFieldSerwer = new JTextField();
		contentPane.add(textFieldSerwer, "flowx,cell 2 1,alignx left");
		textFieldSerwer.setColumns(15);
		
		lblNewLabel = new JLabel("Port");
		contentPane.add(lblNewLabel, "cell 2 1");
		
		textFieldPort = new JTextField();
		contentPane.add(textFieldPort, "cell 2 1,alignx left");
		textFieldPort.setColumns(5);
		
		lblNick = new JLabel("Nick");
		contentPane.add(lblNick, "cell 1 2 1 2,alignx trailing");
		
		textFieldNick = new JTextField();
		contentPane.add(textFieldNick, "cell 2 2 1 2,alignx left");
		textFieldNick.setColumns(10);
		
		chckbxSLL = new JCheckBox("SSL");
		chckbxSLL.setHorizontalAlignment(SwingConstants.LEFT);
		contentPane.add(chckbxSLL, "flowx,cell 2 4,alignx left");
		
		chckbxSzyfrowaniePakietow = new JCheckBox("Szyfrowanie pakiet\u00F3w");
		chckbxSzyfrowaniePakietow.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				lblKlucz.setVisible(chckbxSzyfrowaniePakietow.isSelected());
				textFieldKlucz.setVisible(chckbxSzyfrowaniePakietow.isSelected());
			}
		});
		chckbxSzyfrowaniePakietow.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(chckbxSzyfrowaniePakietow, "cell 2 5,alignx left");
		
		lblKlucz = new JLabel("Klucz");
		lblKlucz.setVisible(false);
		contentPane.add(lblKlucz, "flowx,cell 2 6,alignx left");
		
		textFieldKlucz = new JTextField();
		textFieldKlucz.setVisible(false);
		contentPane.add(textFieldKlucz, "cell 2 6,growx");
		textFieldKlucz.setColumns(10);
		
		btnPolacz = new JButton("Polacz");
		btnPolacz.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String host = textFieldSerwer.getText();
				int port = Integer.parseInt(textFieldPort.getText());
				String nick = textFieldNick.getText();
				
				if(mLogOnListener != null) {
					
					LogOnArguments args;
					
					if(chckbxSzyfrowaniePakietow.isSelected()) {
						args = new LogOnArguments(host, port, nick, false, textFieldKlucz.getText());
					} else {
						args = new LogOnArguments(host, port, nick);
					}
					
					mLogOnListener.logOn(args);
				}
			}
		});
		contentPane.add(btnPolacz, "cell 2 8");
	}
	
	public void setLonOnListener(LogOnListener listener) {
		mLogOnListener = listener;
	}

}
